package com.qdaily.ui.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QDJsonUtil;
import com.qdaily.supportlibrary.views.list.ListViewDataAdapter;
import com.qdaily.ui.QDBaseFragment;
import com.qdaily.ui.R;
import com.qdaily.ui.controller.ActivityController;
import com.qdaily.ui.data.Constants;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.QDEnum;
import com.qdaily.ui.net.entity.MyCommentEntity;
import com.qdaily.ui.net.model.MyCommentMeta;
import com.qdaily.ui.ui.MySendCommentsActivity;
import com.qdaily.ui.ui.viewholder.MyDynaCommentsViewHolder;
import com.qdaily.ui.ui.views.OnRefreshAndLoadMoreListener;
import com.qdaily.ui.ui.views.QDRefreshListView;

/**
 * 我的动态，别人评论我的。
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/23/15
 * Time: 01:55
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class MyCommentsFragment extends QDBaseFragment implements ListView.OnItemClickListener,
        OnRefreshAndLoadMoreListener, View.OnClickListener {
    private QDRefreshListView qdRefreshListView = null;
    private View headerView = null;
    private ListViewDataAdapter<MyCommentMeta> adapter;
    private boolean isHasMore;
    private boolean isLoading;
    private boolean isLastListEmpty;
    private int mLastTime;

    @Override
    protected int getLayoutId() {
        return R.layout.view_pullrefresh_listview;
    }

    @Override
    protected void setupViews(View view) {
        qdRefreshListView = (QDRefreshListView) view.findViewById(R.id.qdRefreshListView);
        headerView = LayoutInflater.from(getContext()).inflate(R.layout.view_my_dyna_headerview, null);
        qdRefreshListView.setOnRefreshAndLoadMoreListener(this);
        qdRefreshListView.setOnItemClick(this);
        headerView.setOnClickListener(this);
    }

    @Override
    protected void setupData() {
        qdRefreshListView.addHeaderView(headerView);
        adapter = new ListViewDataAdapter<>();
        adapter.setViewHolderClass(this, MyDynaCommentsViewHolder.class);
        qdRefreshListView.setAdapter(adapter);
    }

    private void requestMycomments(int lasTime) {
        if (isLoading)
            return;
        isLoading = true;
        getRequestManager().requestCommentOnMy(lasTime);
    }

    @Override
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
        super.onRequestCallBack(params, response);
        isLoading = false;
        if (params instanceof NetConfigs.RequestCommentOnMy) {
            MyCommentEntity commentsEntity = QDJsonUtil.Json2Object(response.toString(), MyCommentEntity.class);
            if (commentsEntity.getMeta().getStatus() == NetConfigs.HTTP_RESPONSE_200 &&
                    null != commentsEntity.getResponse().getComments() &&
                    commentsEntity.getResponse().getComments().getList().size() > 0) {

                NetConfigs.RequestCommentOnMy requestCommentOnMy = (NetConfigs.RequestCommentOnMy) params;
                if (requestCommentOnMy.getLastTime() == 0) {
                    adapter.getDataList().clear();
                }

                isHasMore = commentsEntity.getResponse().getComments().isHas_more();
                mLastTime = commentsEntity.getResponse().getComments().getLast_time();
                isLastListEmpty = commentsEntity.getResponse().getComments().getList().size() == 0;

                adapter.getDataList().addAll(commentsEntity.getResponse().getComments().getList());
                adapter.notifyDataSetChanged();
            } else {
                displayToast(commentsEntity.getMeta().getMsg());
            }
            qdRefreshListView.onRefreshComplete();
        }
    }

    @Override
    protected void onRequestErrorCallBack(NetUtil.NetParams params, Object response) {
        isLoading = false;
        qdRefreshListView.hasMoreLoadData(true, true);
    }

    @Override
    public void onClick(View v) {
        if (v == headerView) {
            getContext().startActivity(new Intent(getContext(), MySendCommentsActivity.class));
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyCommentMeta myCommentMeta = (MyCommentMeta) parent.getAdapter().getItem(position);
        if (Constants.IGenreType.isVote(myCommentMeta.getGenre())) {
            ActivityController.DisplayVote(getContext(), myCommentMeta.getType_id());
        } else if (Constants.IGenreType.isMob(myCommentMeta.getGenre())) {
            ActivityController.DisplayMob(getContext(), myCommentMeta.getType_id());
        } else if (Constants.IGenreType.isArticle(myCommentMeta.getGenre())) {
            ActivityController.DisplayArticle(getContext(), myCommentMeta.getType_id());
        }
    }

    @Override
    public void onRefreshBegin() {
        requestMycomments(0);
    }

    @Override
    public void onLoadMore() {
        if (isHasMore) {
            requestMycomments(mLastTime);
        }
    }

    @Override
    public void onUIRefreshComplete() {
        qdRefreshListView.hasMoreLoadData(isLastListEmpty, isHasMore);
    }
}
